#include "giftcard.h"
#include <QSqlQuery>
#include <QDateTime>
#include <QDebug>
#include <QObject>
#include <QThread>

//const int GiftCard::ID_COL = 0;
//const int GiftCard::GIFT_NUMBER_COL = 1;
//const int GiftCard::OUT_DATE = 2;
//const int GiftCard::IN_DATE = 3;

GiftCard::GiftCard()
{
}

bool GiftCard::insertGiftCard(int number, float value, QDate outDate){

    QString query_str;
    QDateTime outDateTime;
    outDateTime.setDate(outDate);

    int outDateTimestamp = outDateTime.toTime_t();
    QSqlQuery *sqlQuery = new QSqlQuery();
    query_str = "INSERT INTO gift_card (number, value, out_date, expend) values("
            + QString::number(number) + "," + QString::number(value, 'f', 2) + "," + QString::number(outDateTimestamp) +
            ",0.00)";
    qDebug()<< query_str;
    if (!sqlQuery->exec(query_str)){
        return false;
    }else{
        return true;
    }
}

bool GiftCard::updateGiftCard(int number, float value, QDate inDate){
    QString query_str;
    QDateTime inDateTime;
    inDateTime.setDate(inDate);
    int inDateTimestamp = inDateTime.toTime_t();
    QSqlQuery *sqlQuery = new QSqlQuery();

    query_str = "Update gift_card set in_date="
            + QString::number(inDateTimestamp) + "," + "expend = expend + " + QString::number(value, 'f', 2) +
            " where number=" + QString::number(number);
    qDebug()<< query_str;
    if (!sqlQuery->exec(query_str)){
        return false;
    }else{
        return true;
    }
}

